package fdoj.eventInvitations.servlet;

import java.io.*;
import java.sql.*;
import java.util.UUID;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

import fdoj.eventInvitations.dao.PersonDao;
import fdoj.eventInvitations.entity.Person;
import fdoj.eventInvitations.util.SendMail;

public class Registration extends HttpServlet {

	@EJB
	private PersonDao personDao;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {

			String firstName= request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String confirmPassword = request.getParameter("confirmPassword");
			String phoneNumber = request.getParameter("phoneNumber");

			if(firstName==null||firstName.trim()==""){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(lastName==null||lastName.trim()==""){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(email==null||email.trim()==""){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(password==null||password.trim()==""){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(confirmPassword==null||confirmPassword.trim()==""){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(!password.equals(confirmPassword)){
				forward(firstName,lastName,email,phoneNumber,"",request);
				request.setAttribute("passwordMismatch", true);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}

			if(personDao.findByEmail(email)!=null){
				forward(firstName,lastName,email,phoneNumber,password,request);
				request.setAttribute("emailExists", true);
				request.getRequestDispatcher("/registrationRetry.jsp").forward(request, response);
				return;
			}
			UUID uuid = UUID.randomUUID();
			Person p=new Person();
			p.setEmail(email);
			p.setFirstName(firstName);
			p.setLastName(lastName);
			p.setPhoneNum(phoneNumber);
			p.setPassword(password);
			p.setRegToken(uuid.toString());
			personDao.persist(p);
			request.getSession().setAttribute("loggedUser", p);
			response.sendRedirect(response.encodeRedirectURL("./index"));
			String text =  "Kliknite na link kako bi zavrsili registraciju: " +
							"http://127.0.0.1:8080/PozivNaDogadjaj/reg?id=" + p.getId() + "&regToken="+p.getRegToken();
			String subject = "Registracija";
			SendMail sm = new SendMail();
			sm.Send(email, text, subject);
			return;

		} catch (Exception e) {
			e.printStackTrace();

		}
		response.sendRedirect(response.encodeRedirectURL("./error.jsp"));
	}

	private void forward(String firstName,String lastName,String email,String phoneNumber,String password,HttpServletRequest request){
		request.setAttribute("firstName", firstName);
		request.setAttribute("lastName", lastName);
		request.setAttribute("email", email);
		request.setAttribute("phoneNumber", phoneNumber);
		request.setAttribute("password", password);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}